package com.yq.websocket.socket;

import android.content.Context;
import android.util.Log;

import com.yq.websocket.Utils;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.net.InetSocketAddress;

public class SocketServer extends WebSocketServer {

    private static WebSocketServer socketServer;
    public static boolean startSuccess = false;
    private static ServerStateListener listener;

    SocketServer(InetSocketAddress host) {
        super(host);
    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        Log.d("WebSocketServer", "onOpen()：连接到: " + getRemoteSocketAddress(conn));
//        isServer = true;
        if (listener != null) {
            listener.onOpen(conn, handshake);
        }
    }

    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        Log.d("WebSocketServer", "onClose");
        if (listener != null) {
            listener.onClose(conn, code, reason, remote);
        }
    }

    @Override
    public void onMessage(WebSocket conn, String message) {
        Log.d("WebSocketServer", "onMessage" + message);
        if (listener != null) {
            listener.onMessage(conn, message);
        }
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {
        Log.d("WebSocketServer", "onError" + ex.toString());
        if (listener != null) {
            listener.onError(conn, ex);
        }
    }

    @Override
    public void onStart() {
        Log.d("WebSocketServer", "onStart:" + socketServer.getAddress());
        startSuccess = true;
        if (listener != null) {
            listener.onStart();
        }
    }

    public static void startService(Context context, int port) {
        String ip = Utils.getIPAddress(context);
        InetSocketAddress myHost = new InetSocketAddress(ip, port);
        SocketServer webSocketServer = new SocketServer(myHost);
        try {
            webSocketServer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        SocketServer.socketServer = webSocketServer;
    }

    public static void stopService() {
        try {
            socketServer.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendMsg(String string) {
        try {
            socketServer.broadcast(string);
            //websocketServer.broadcast(string,clients);//Collection<WebSocket> clients,指定发送到哪个客户端
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setListener(ServerStateListener l) {
        listener = l;
    }

    public static class ServerStateListener {

        public void onOpen(WebSocket conn, ClientHandshake handshake) {
        }

        public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        }

        public void onMessage(WebSocket conn, String message) {
        }

        public void onError(WebSocket conn, Exception ex) {
        }

        public void onStart() {
        }
    }
}